<?php
class UserController extends AdminBaseController   {
	public function lists(){
		$user = HttpServerManager::getUserServer();
		$data = $user->pagingBySearch($_GET);
		$status = Common::_t('confirm_status');
		$user_type = Common::_t('user_type');
		$register_type = Common::_t('register_type');
		$lead_source = Common::_t('lead_source');
		$user_kind = Common::_t('user_kind');
		
		foreach ($data['list'] as $k=>$v){
			$data['list'][$k]['status'] = $status[$v['status']];
			$data['list'][$k]['user_type_name'] = $user_type[$v['user_type']];
		}
		
		/**
		 页面标签使用
		 */
		$page_status_option = $status;
		$data['page_status_option']= $page_status_option;
		$data['page_user_type_option']= $user_type;
		$data['page_register_type_option']= $register_type;
		$data['page_lead_source_option']= $lead_source;
		$data['page_user_kind_option']= $user_kind;
		
		
		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$level_pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}
		unset($level_list);
		$data['level_pair'] = $level_pair;
		
		return $data;
	}
	
	public function sendNotifySms(){//每次发送5条,1分钟发送一次
		//获取需要发短信的预约表,每次提取10个发送
		$where = array();
		$where['status'] = SEND_STATUS_0;
		$sms_send_list = HttpDaoManager::getMobileSendlistDao()->searchList($where,'',10);
		if(!empty($sms_send_list)){
			foreach($sms_send_list as $key=>$value){
				$target_id  = $value['target_id'];
				$id = $value['id'];
				if(isset($value['phone'])&&!empty($value['phone'])){
					$sms_send_status = HttpServerManager::getMobileSendlistServer()->sendSMS($value['phone'],$value['content']);
					$sms_send_array = json_decode($sms_send_status,true);
					if($sms_send_array['result']==="0"){//成功
						$updata = array();
						$updata['status'] = SEND_STATUS_1;
						HttpDaoManager::getMobileSendlistDao()->update($updata,$id);
						unset($updata['status']);
						$updata['sms_notify'] = SEND_STATUS_1;
						HttpDaoManager::getBookCourseDao()->update($updata,$target_id);
					}else{//失败
						$updata = array();
						$updata['status'] = SEND_STATUS_2;
						HttpDaoManager::getMobileSendlistDao()->update($updata,$id);
						unset($updata['status']);
						$updata['sms_notify'] = SEND_STATUS_1;
						HttpDaoManager::getBookCourseDao()->update($updata,$target_id);
					}
				}
			}
		}
		exit;		
	}
	
	public function getUserBookMobileSendList(){//每次发送5条,1分钟发送一次
		//获取需要发短信的预约表,每次提取10个发送
		HttpServerManager::getBookCourseServer()->getUserNofityBookList();
		exit;		
	}
	
	public function autoUpdateTeacherStar(){
		//获取需要发短信的预约表,每次提取10个发送,初始选择等级时候，将类似于给了初始分数
		$teacherMxInfo = HttpDaoManager::getTeacherLogDao()->getTeacherMxSumInfo($where);
		$teacherRecacluteInfo = array();
		$teacherChangeIntegralInfo = array();
		foreach($teacherMxInfo as $key=>$value){
			$teacherRecacluteInfo[$value['teacher_id']] =$value['base_integral']+$value['integral'];
			if($teacherRecacluteInfo[$value['teacher_id']]>0&&$teacherRecacluteInfo[$value['teacher_id']]<=200){
			   $new_real_education_experience =  EDUCATION_EXPERIENCE_1;
			   if($new_real_education_experience==$value['real_education_experience']){
			   		continue;
			   }else{
			   		$teacherChangeIntegralInfo[$value['teacher_id']] = $new_real_education_experience;
			   }
			}elseif($teacherRecacluteInfo[$value['teacher_id']]>=201&&$teacherRecacluteInfo[$value['teacher_id']]<=400){
				$new_real_education_experience =  EDUCATION_EXPERIENCE_2;
				if($new_real_education_experience==$value['real_education_experience']){
					continue;
				}else{
					$teacherChangeIntegralInfo[$value['teacher_id']] = $new_real_education_experience;
				}			
			}elseif($teacherRecacluteInfo[$value['teacher_id']]>=401&&$teacherRecacluteInfo[$value['teacher_id']]<600){
				$new_real_education_experience =  EDUCATION_EXPERIENCE_3;
				if($new_real_education_experience==$value['real_education_experience']){
					continue;
				}else{
					$teacherChangeIntegralInfo[$value['teacher_id']] = $new_real_education_experience;
				}				
			}elseif($teacherRecacluteInfo[$value['teacher_id']]>=601&&$teacherRecacluteInfo[$value['teacher_id']]<=1500){
				$new_real_education_experience =  EDUCATION_EXPERIENCE_4;
				if($new_real_education_experience==$value['real_education_experience']){
					continue;
				}else{
					$teacherChangeIntegralInfo[$value['teacher_id']] = $new_real_education_experience;
				}				
			}elseif($teacherRecacluteInfo[$value['teacher_id']]>=1500&&$teacherRecacluteInfo[$value['teacher_id']]<=3000){
				$new_real_education_experience =  EDUCATION_EXPERIENCE_5;
				if($new_real_education_experience==$value['real_education_experience']){
					continue;
				}else{
					$teacherChangeIntegralInfo[$value['teacher_id']] = $new_real_education_experience;
				}			
			}
		}
		
		foreach($teacherChangeIntegralInfo as $key=>$value){
			$data = array();
			$data['real_education_experience'] = $value;
			HttpDaoManager::getTeacherDao()->update($data, $key);
		}
		exit;
	}
		
	public function add(){
		$data = array();
		if (!empty($_GET['id'])){
			$data = HttpDaoManager::getUserDao()->get($_GET['id']);
		}
		$sex  = Common::_t('sex');
		$job_type = Common::_t('user_job');
		$leadSource = Common::_t('lead_source');
		
		$data['leadSourceOptions'] = page_modifier_options_ext($leadSource,getStr($data,'lead_source') );
		
		$data['sex'] = isset($data['sex'])?$data['sex']:'';
		$data['sexOptions'] = page_modifier_options($sex,$data['sex']);
		$data['status'] = isset($data['status'])?$data['status']:0;
		
		$status = Common::_t('user_status');
		$data['statusOptions'] = page_modifier_options($status,$data['status']);

		$user_type = Common::_t('user_type');
		//$data['userTypeOptions'] = page_modifier_options($user_type,$data['user_type']);
		$data['user_type_pair'] = $user_type;

		$user_kind_pair = Common::_t('user_kind');
		//$data['userTypeOptions'] = page_modifier_options($user_type,$data['user_type']);
// 		$data['user_kind_pair'] = $user_kind;
		$targetCourse = Common::_t('target_course');
		$targetCourseOptions = page_modifier_options($targetCourse,getStr($data,'target_course'));
		$data['targetCourseOptions'] = $targetCourseOptions;
		
		$province = Common::getStrValue($data,'province');
		$city =Common::getStrValue($data,'city');
		$data['select'] = HttpServerManager::getRegionServer()->cityProvinceSelect($province, $city,'province','city');
		$employees = HttpDaoManager::getEmployeesDao()->getListByDepartment(Common::_t('faculty_member'));
		$tmps=array();
		foreach ($employees as $v){
			$tmps[$v['id']] = $v['name'];
		}
		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$level_pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				//$level_pair[$v['id']]='Level'.'-'.$v['level'].':'.$v['subject'];
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}
		unset($level_list);


		$funduser_pair = array();
		$funduser_list =  HttpDaoManager::getFunduserDao()->searchList();
		foreach($funduser_list as $key=>$value){
			$funduser_pair[$value['id']] = $value['serial_number'].'-'.$value['name'];
		}		
		
		$data['id'] = Common::getGetStrValue('id');
		$data['trackPeopleSelect'] =  page_modifier_options($tmps,common::getStrValue($data, 'employee_id'));
		$data['userJobOptions'] = page_modifier_options($job_type,common::getStrValue($data, 'job'));
		$data['userLevelOptions'] = page_modifier_options($level_pair,common::getStrValue($data, 'level'));
		$data['page_funduser_options'] = page_modifier_options_ext($funduser_pair,common::getStrValue($data, 'fund_user_id'));
		$data['page_user_kind_options'] = page_modifier_options_ext($user_kind_pair,common::getStrValue($data, 'user_kind'));
		
		return $data;
	}
	public function subAdd(){
		$tmp = HttpServerManager::getUserServer()->register($_POST);
		if($tmp===TRUE){
			$msg = Common::_t('data_insert_success');
			ClassManager::getToos()->msgbox($msg);
		}else{
			$error = Common::_t('eror_registerbox');
			ClassManager::getToos()->msgbox($error[$tmp]);			
		}
	}
	public function subUpdate(){
		if(isset($_POST['password'])&&$_POST['password']!=""){
			$_POST['password']=md5($_POST['password']);	
		}else{
			unset($_POST['password']);
		}
		if($_POST['fund_user_id']<0){
			$_POST['fund_user_id'] = 0;
		}		
		$msg = Common::_t('data_edit_success');
		HttpDaoManager::getUserDao()->update($_POST,$_POST['id']);
		ClassManager::getToos()->msgbox($msg,'user_lists.html');
	}
	
	public function updateuserByFunduser(){
		$fund_user_id = $_POST['fund_user_id'];
		$user_id = $_POST['user_id'];
		$user_info = HttpDaoManager::getUserDao()->get($user_id);
		$res = array();
		if($user_info['fund_user_id']==0){
			$data['fund_user_id'] = $fund_user_id;
			$msg = Common::_t('data_edit_success');
			HttpDaoManager::getUserDao()->update($data,$user_id);
			$res['success'] ='success';
			exit(json_encode($res));
		}
		$res['success'] ='fail';
		exit(json_encode($res));
	}
	
	public function info(){
		$sex = Common::_t('sex');
		$data =   HttpDaoManager::getUserDao()->get($_GET['id']);
		$data['province_name']="";
		if($data['province']>0){
			$province_info = HttpDaoManager::getRegionDao()->get($data['province']);
			$data['province_name'] = $province_info['name'];
		}
		$data['city_name']="";
		if($data['city']>0){
			$city_info = HttpDaoManager::getRegionDao()->get($data['city']);
			$data['city_name'] = $city_info['name'];
		}
		$data['recommend_user_name'] = "";
		if($data['user_id']>0){
			$recommend_user =   HttpDaoManager::getUserDao()->get($data['user_id']);
			$data['recommend_user_name'] = $recommend_user['english_name'];
		}

		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$level_pair = array();

		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}

		$data['level_name']="";
		if($data['level']>0){
			$data['level_name'] = $level_pair[$data['level']];
		}
		
		$data['lead_source_name'] ="";
		$leadSource = Common::_t('lead_source');
		if($data['lead_source']>0){
			$data['lead_source_name'] = $leadSource[$data['lead_source']];
		}
		$status = Common::_t('user_status');
		$data['status_name'] = $status[$data['status']];
		
		$user_type = Common::_t('user_type');		
		$data['user_type_name'] = $user_type[$data['user_type']];
		
		$where_filter= array();
		$where_filter['user_id']=$data['id'];
		$leterStationList = HttpDaoManager::getLetterStationDao()->getList($where_filter,' order by id desc ',5);
		$data['leterStationList']=$leterStationList;
		$leter_status = Common::_t('letter_station');
		foreach($data['leterStationList'] as $key=>$value){
			$data['leterStationList'][$key]['status_name']=	$leter_status[$value['status']];		
		}
		$data['sex'] = $sex[$data['sex']];
		
		$userBook_filter = array();
		$userBook_filter['user_id'] = $data['id'];
		$data['book'] = HttpServerManager::getBookCourseServer()->userBook($userBook_filter,5);
		$data['order'] = HttpServerManager::getOrderServer()->lists(array('user_id'=>$_GET['id']),'order by id desc',5);
		$data['integralType'] = Common::_t('integral_type');	
		$data['integralLog'] = HttpDaoManager::getUserIntegralLogDao()->searchList(array('user_id'=>$_GET['id'],'type'=>INTEGRAL_TYPE_3),'',5);

		return $data;
	}
	public function detailInfo(){
		$user =   HttpDaoManager::getUserDao()->get($_GET['id']);
		$data['content']= $user['content'];
		unset($user);
		return $data;
	}
	public function smsCodeInfo(){
		$user =   HttpDaoManager::getUserDao()->get($_GET['id']);
		$data['sms_code']= $user['sms_code'];
		$data['sms_verify']= Common::_t('sms_verify');
		$data['id'] = $_GET['id'];
		$data['sms_verify_options'] =   page_modifier_options($data['sms_verify'],common::getStrValue($user, 'real_phone'));
		return $data;
	}
	
	public function updateClassInfo(){
		$user =   HttpDaoManager::getUserDao()->get($_GET['id']);
		$data['id'] = $_GET['id'];
		$data['email']= $user['email'];
		$data['english_name'] = $user['english_name'];
		$data['phone'] = $user['phone'];
		$data['user_class'] = $user['user_class'];
		$data['user_monthly_class'] = $user['user_monthly_class'];
		$data['class_period_validity'] =0;
		
		if($user['class_period_validity']>0){
			$data['class_period_validity'] = date('Y-m-d',$user['class_period_validity']);
		}
		
		$data['monthly_class_period_validity']=0;
		if($user['monthly_class_period_validity']>0){
			$data['monthly_class_period_validity'] = date('Y-m-d',$user['monthly_class_period_validity']);
		}
		
		$user_class_type = Common::_t('user_class_type');
		$data['page_user_class_type_option']  = page_modifier_options($user_class_type,Common::getGetStrValue('user_class_type'));
		
		$user_class_modify_type = Common::_t('user_class_modify_type');
		$data['page_user_class_modify_type_option']  = page_modifier_options($user_class_modify_type,Common::getGetStrValue('user_class_modify_type'));
		
		$user_class_modify_reason = Common::_t('user_class_modify_reason');
		$data['page_user_class_modify_reason_option']  = page_modifier_options($user_class_modify_reason,Common::getGetStrValue('user_class_modify_reason'));
		
		return $data;
	}
	

	public function subUpdateClassInfo(){
		$id = $_POST['id'];
		$user =  HttpDaoManager::getUserDao()->get($id);
		$user_class_type = $_POST['user_class_type'];
		$user_class_modify_type = $_POST['user_class_modify_type'];
		$valid_period_validity = $_POST['valid_period_validity'];
		$modify_value = intval($_POST['modify_value']);
		$modify_reason = $_POST['modify_reason'];
		$remark = "";
		$user_class_type_pair = Common::_t('user_class_type');
		$user_class_type_name = $user_class_type_pair[$user_class_type];
		
		$user_class_modify_type_pair = Common::_t('user_class_modify_type');
		$user_class_modify_type_name = $user_class_modify_type_pair[$user_class_modify_type];

		$user_class_modify_reason_pair = Common::_t('user_class_modify_reason');
		$user_class_modify_reason_name = $user_class_modify_reason_pair[$modify_reason];
		
		$remark ="";
		if($user_class_type==USER_CLASS){
			if($user_class_modify_type==USER_CLASS_MODIFY_TYPE_ADD_CLASS){
				$user_class = $user['user_class']+$modify_value;
				$data = array();
				$data['user_class'] = $user_class;
				$data['class_period_validity'] = strtotime($valid_period_validity)+86399;
				HttpDaoManager::getUserDao()->update($data, $id);
				$remark =$user_class_type_name.'由'.$user['user_class'].$user_class_modify_type_name.$modify_value.'次,'.$user_class_modify_reason_name.',调整有效期至:'.$valid_period_validity;
			}
// 			if($user_class_modify_type==USER_CLASS_MODIFY_TYPE_ADD_TIME){
// 				if($user['class_period_validity']>0){
// 					$class_period_validity = $user['class_period_validity']+$modify_value*86399;
// 				}else{
// 					$class_period_validity = strtotime(date('Y-m-d'))+($modify_value+1)*86399;
// 				}
// 				$data = array();
// 				$data['class_period_validity'] = $class_period_validity;
// 				HttpDaoManager::getUserDao()->update($data, $id);
// 			}
			
		}
		
		if($user_class_type==USER_MONTHLY_CLASS){
			if($user_class_modify_type==USER_CLASS_MODIFY_TYPE_ADD_CLASS){
				$data = array();
				$data['user_monthly_class'] = $user['user_monthly_class']+$modify_value;
				$data['monthly_class_period_validity'] = strtotime($valid_period_validity)+86399;
				HttpDaoManager::getUserDao()->update($data, $id);
				$remark =$user_class_type_name.'由'.$user['user_monthly_class'].$user_class_modify_type_name.$modify_value.'次,'.$user_class_modify_reason_name.',调整有效期至:'.$valid_period_validity;
			}
		}
		
// 		if($user_class_modify_type==USER_CLASS_MODIFY_TYPE_ADD_CLASS){
// 		$remark =$user_class_type_name.$user_class_modify_type_name.$modify_value.'天'.'-'.$user_class_modify_reason_name;
		
		$logData = array();
		$logData['type'] = $modify_reason;
		$logData['target_id'] = 0;
		$logData['add_time'] = time();
		$logData['add_user'] = $_SESSION['admin']['name'];
		$logData['class_hour'] = $modify_value;
		$logData['user_id'] = $id;
		$logData['remark'] = $remark;
		HttpDaoManager::getUserClassHourLogDao()->add($logData);
		$res = array();
		$res['success'] ='success';
		exit(json_encode($res));
	}
			
	
	public function sendCode(){
		$user_id = $_POST['id'];
		$code = $_POST['code'];
		$user = HttpDaoManager::getUserDao()->get($user_id);
		$phone = $user['phone'];
		$content = '验证码：'.$code;

		$sms_send_status = HttpServerManager::getMobileSendlistServer()->sendSMS($phone,$content);
		$sms_send_array = json_decode($sms_send_status,true);
		$sendResult = array();
		if($sms_send_array['result']==="0"){
			$id = $user_id;
			$data = array();
			$data['sms_code']= $code;
			HttpDaoManager::getUserDao()->update($data, $id);
			$sendResult['success'] = 'success';
			exit(json_encode($sendResult));
		}
		$sendResult['success'] = 'fail';
		exit(json_encode($sendResult));
	}
	
	
	public function saveSmsCode(){
		$user_id = $_POST['id'];
		$code = $_POST['code'];
		$real_phone = $_POST['real_phone'];
		
		$id = $user_id;
		$data = array();
		$data['sms_code']= $code;
		$data['real_phone']= $real_phone;

		HttpDaoManager::getUserDao()->update($data, $id);
		$sendResult['success'] = 'success';
		exit(json_encode($sendResult));
	}
	
	public function delete(){
		$msg = Common::_t('data_del_success');
		HttpServerManager::getUserServer()->deleteBatchByid($_GET['id']);
		ClassManager::getToos()->msgbox($msg);
	}
	
	public function send(){
		$type=array(MESSAGE_TYPE_LETTER,MESSAGE_TYPE_EMAIL,MESSAGE_TYPE_MOBILE);
		foreach ($type as $v){
			$template[$v] = HttpDaoManager::getSendInfoTemplateDao()->getListByType($v);
		}
		$data['template']=$template;
		
		/**
		页面标签使用
		 */
		$page_msg_option = Common::_t('msg_type');
		$page_msg_selected = MESSAGE_TYPE_MOBILE;

		$data['page_msg_option']= $page_msg_option;
		$data['page_msg_selected']= $page_msg_selected;
		
		return $data;
	}
	public function subSend(){
		$where = array();
		if (Common::getPostStrValue('start_time')!=""){
			$where['start_time']=strtotime($_POST['start_time']);
		}
		if (Common::getPostStrValue('over_time')!=""){
			$where['over_time']=strtotime($_POST['over_time']);
		}
		if(Common::getPostStrValue('english_name')!=""){
			$where['english_name']=$_POST['english_name'];
		}
		if(Common::getPostStrValue('phone')!=""){
			$where['phone']=$_POST['phone'];
		}
		if(Common::getPostStrValue('email')!=""){
			$where['email']=$_POST['email'];
		}
		
		$where['way'] = $_POST['way'];
		$tmp = HttpServerManager::getUserServer()->userSend($where, $_POST);
		if ($tmp===true){
			$msg = Common::_t('msg_send_success');
			ClassManager::getToos()->msgbox($msg);
		}
		else{
			$error_msgbox = Common::_t('eror_msgbox');
		
			ClassManager::getToos()->msgbox($error_msgbox[$tmp]);
		}
	}
	public function updateTrackPeople(){
		$data['batchId']  = implode(',',$_GET['id']);
		$employees = HttpDaoManager::getEmployeesDao()->getListByDepartment(Common::_t('faculty_member'));
		$tmps =array();
		foreach ($employees as $v){
			$tmps[$v['id']] = $v['name'];
		}
		$data['trackPeopleSelect'] =   page_modifier_options($tmps);
		return $data;
	}
	public function subUpdateTrackPeople(){
		HttpDaoManager::getUserDao()->batchUpdateTrackPeople($_POST['employee_id'], $_POST['batchId']);
		$msg = Common::_t('data_edit_success');
		ClassManager::getToos()->msgbox($msg,"User_lists.html");
	}
	
	public function overdueEmpty(){
		HttpServerManager::getUserServer()->overdueEmpty();
	}
	
	public function getUser(){
		$user_name = $_POST['user_name'];
		$where = array();
		$where['email'] = $user_name;
		$user_info = HttpDaoManager::getUserDao()->searchList($where);
		if(empty($user_info)){//如果为空，再找一遍
			$where['phone'] = $userr_name;
			$user_info = HttpDaoManager::getUserDao()->searchList($where);
		}
		
		$result= array();
		if(empty($user_info)){//找不到
			$result = array('success'=>'fail','num'=>0)	;
			$where['email'] = $user_name;
		}else{//找到了
			$num = count($user_info);
			if($num>1){
				$result = array('success'=>'fail','num'=>$num);
			}
			$result = array('success'=>'success','num'=>$num,'user_info'=>$user_info[0]);
		}
		exit(json_encode($result));
	}	
}
?>